//处理提交表单返回的数据
var successCallback = function (data) {
    window.location.href = '/Home/BookPage/index.html';
	
};
//  AES 加密
var AESencrypt = function (data) {
    var key  = CryptoJS.enc.Latin1.parse('1234123412341324');
	var iv   = CryptoJS.enc.Latin1.parse('1234123412341324');
	//加密
	var encrypted = CryptoJS.AES.encrypt(data,key,{iv:iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.ZeroPadding});
	return encrypted.toString();
};

$(function () {
	// 记住密码
	var cookieTagForLoginAccount = 'mz_login_account';
    var cookieTagForLoginPassword = 'mz_login_password';
    var loginAccount = Cookies.get(cookieTagForLoginAccount);
    var loginPassword = Cookies.get(cookieTagForLoginPassword);
    if(!loginAccount){
		$('#rememberUserName').prop('checked', false);
    }else{
		$('#rememberUserName').prop('checked', true);
    }

    if($('#rememberUserName').prop('checked')){
        $('#requestUsername').val(loginAccount?loginAccount:'');
        $('#requestPassword').val(loginPassword?loginPassword:'');
	}else{
        $('#requestUsername').val('');
        $('#requestPassword').val('');
	}

	$('#rememberUserName').on('change', function(e){
		if ($(this).prop('checked')){
			requestUsername = $('#requestUsername');
			requestPassword = $('#requestPassword');
			var password = requestPassword.val();
			// 是否加密过
			if (password.length <= 20) {
				password = AESencrypt(password);
			}
			Cookies.set(cookieTagForLoginAccount, requestUsername.val(), { expires: 90 });
			Cookies.set(cookieTagForLoginPassword, password, { expires: 90 });
		}else{
			Cookies.set(cookieTagForLoginAccount, '', { expires: -1 });
			Cookies.set(cookieTagForLoginPassword, '', { expires: -1 });
		}
	});

	$('#masterLogin').click(function(){
		$('#meizhuLogin').css('color','#1f1f1f');
		$(this).css('color','#ff6e40');
		$('#meizhuGroup').hide();
		$('#masterGroup').show();
		$("#requestSubmit").text(lang('login.sync'));
		$('#requestSubmit').attr('masterSyn','1');
		$('.apply-tip .apply').addClass('hidden');
	});
	$('#meizhuLogin').click(function(){
		$('#masterLogin').css('color','#1f1f1f');	
		$(this).css('color','#ff6e40');
		$('#masterGroup').hide();
		$('#meizhuGroup').show();
		$("#requestSubmit").text(lang('login'));
		$('#requestSubmit').attr('masterSyn','0');
        $('.apply-tip .apply').removeClass('hidden');
	});

	// 是否加密过
	if (loginPassword && $('#requestPassword').val().length > 23) {
		$('#login-container').inputValidate({
	        submit: {
	            query: '#requestSubmit'
	        },
	        inputs: [{
	            query: '#requestUsername',
	            regexp: window.commonRegexp.mobile,
	            message: lang('tip.mobile'),
	            emptyMessage: lang('tip.mobileNull')
	        }]
	    });
	} else {
		$('#login-container').inputValidate({
	        submit: {
	            query: '#requestSubmit'
	        },
	        inputs: [{
	            query: '#requestUsername',
	            regexp: window.commonRegexp.mobile,
	            message: lang('tip.mobile'),
	            emptyMessage: lang('tip.mobileNull')
	        }, {
	            query: '#requestPassword',
	            regexp: window.commonRegexp.password,
	            message: lang('tip.password'),
	            emptyMessage: lang('tip.passwordNull')
	        }]
	    });
	}

    $('#requestSubmit').on('successSubmit.amos.inputValidate', function (e) {
        var submitBtn = $(this);

        var isRemember = $('#rememberUserName').prop('checked');
		if(isRemember){
			var password = $('#requestPassword').val();
			// 是否加密过
			if (password.length <= 20) {
				password = AESencrypt(password);
			}
			Cookies.set(cookieTagForLoginAccount, $('#requestUsername').val(), { expires: 90 });
			Cookies.set(cookieTagForLoginPassword, password, { expires: 90 });
		}else{
			Cookies.set(cookieTagForLoginAccount, '', { expires: -1 });
			Cookies.set(cookieTagForLoginPassword, '', { expires: -1 });
		}

        // 该处可放ajax请求
        submitBtn.attr('disabled', true);
        var params = {};
        params.mobile = $('#requestUsername').val();
        params.password = $('#requestPassword').val();
        params.areaCode = $('.areaCodeValue').text().trim();
		var masterSyn = $('#requestSubmit').attr('masterSyn');
		if(masterSyn == '1'){
			ajaxRequest('/Home/Public/syn', params, 'post',function(data){
				time = Date.parse(new Date());
				window.location.href = '/Home/SynPage/community.html?time='+time;
			}, null, submitBtn);
		}else{
			ajaxRequest('/Home/Public/login', params, 'post', successCallback, null, submitBtn);
		}
    });
});
